import '../manger/network_utils.dart';
import '../models/yh_info_model.dart';
import '../manger/user_manger.dart';

class LoadCouponInfoResult {
  final bool success;
  final List<CouponDeListModel>? couponInfoModel;

  LoadCouponInfoResult({required this.success, this.couponInfoModel});
}

class DiscountMangerInfoResult {
  final bool success;
  final List<CouponListModel>? discountModel;
  final List<CouponListModel>? discountModel2;

  DiscountMangerInfoResult({required this.success, this.discountModel, this.discountModel2});
}

class CouponInfoManager {
  static final CouponInfoManager _instance = CouponInfoManager._internal();
  factory CouponInfoManager() => _instance;
  CouponInfoManager._internal();

  // 存储数据
  List<CouponDeListModel>? _couponListModel;

  List<CouponDeListModel>? get couponInfoModel => _couponListModel;

  // 加载数据
  Future<LoadCouponInfoResult> loadInfoData() async {
    try {
      final response = await NetworkUtils.get(
        UserInfos.shared.token.isNotEmpty ? RequestCmd.couponsList2 : RequestCmd.couponsList, 
        params: {'itemId': 0}, // 将 itemId 作为查询参数传入
      );

      final model = CouponDeModel.fromJson(response);

      if (model.code == 200) {
        _couponListModel = model.data;

        return LoadCouponInfoResult(success: true, couponInfoModel: model.data);
      } else {
        return LoadCouponInfoResult(success: false);
      }
      } catch (e) {
        return LoadCouponInfoResult(success: false);
      }
  }
}
class DiscountManger {
  static final DiscountManger _instance = DiscountManger._internal();
  factory DiscountManger() => _instance;
  DiscountManger._internal();

  // 存储数据
  List<CouponListModel>? _discountModel;
  List<CouponListModel>? _discountModel2;

  List<CouponListModel>? get discountModel => _discountModel;
  List<CouponListModel>? get discountModel2 => _discountModel2;

  // 加载数据
  Future<DiscountMangerInfoResult> loadDiscountData(int spuid) async {
    try {
      final response = await NetworkUtils.post(
        RequestCmd.myCouponV1, 
        body: {"spuId": spuid,"num": 1},
      );

      final model = CouponModel.fromJson(response);
      if (model.code == 200) {
        _discountModel = model.data;
        _discountModel2 = model.data;
        return DiscountMangerInfoResult(success: true, discountModel: model.data,discountModel2: model.data);
      } else {
        return DiscountMangerInfoResult(success: false);
      }
      } catch (e) {
        return DiscountMangerInfoResult(success: false);
      }
  }
}


class MyCouponMangerInfoResult {
  final bool success;
  final List<CouponListModel>? myCoupon;

  MyCouponMangerInfoResult({required this.success, this.myCoupon});
}

class MyCouponManger {
  static final MyCouponManger _instance = MyCouponManger._internal();
  factory MyCouponManger() => _instance;
  MyCouponManger._internal();

  // 存储数据
  List<CouponListModel>? _myCoupon;

  List<CouponListModel>? get myCoupon => _myCoupon;

  // 加载数据
  Future<MyCouponMangerInfoResult> loadDiscountData(int status, {int? spuId, int? num}) async {
    try {
      final response = await NetworkUtils.post(
        RequestCmd.myCoupon, 
        body: {"status": status, if (spuId != null) "spuId": spuId, if (num != null) "num": num},
      );

      final model = CouponModel.fromJson(response);

      if (model.code == 200) {
        _myCoupon = model.data;
        return MyCouponMangerInfoResult(success: true, myCoupon: model.data);
      } else {
        return MyCouponMangerInfoResult(success: false);
      }
    } catch (e) {
      return MyCouponMangerInfoResult(success: false);
    }
  }
}